<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/** Trainings models **/
class Trainings extends CI_Model {
    
    private $categories = 'training_categories';
    private $training   = 'training';
    private $users      = 'employee';
	private $files      = 'training_files';
    
    function getTrainingCategories()
	{
		$sql = "SELECT * 
                FROM ".$this->categories;
        $Q=$this->db->query($sql);        
		return $Q->result_array();
	}
	
	function getTitleCategories($id){
		$sql = "SELECT * 
                FROM ".$this->categories;
		$sql .= " WHERE category_id=".$id;		
        $Q=$this->db->query($sql);        
		$value = $Q->row_array();
		return $value['category_name'];
	}
    
    function getDataSingleRecord($id='',$cat='',$main='',$year='',$month='',$day=''){
		$this->db->select("*,u.EmployeeNickName as user_name,DAY(training_date) as day");
		$this->db->select("MONTHNAME(training_date) as month,YEAR(training_date) as year");
		$this->db->select('MONTHNAME(training_date) as month_name');
		$this->db->join($this->users.' u','u.EmployeeID=t.EmployeeID','INNER');
		if($id)
			$this->db->where('training_id',$id);
		if($cat)
			$this->db->where('training_category',$cat);
		if($year):
			$this->db->where('YEAR(training_date)',$year);
			$this->db->where('MONTH(training_date)',$month);
			$this->db->where('DAY(training_date)',$day);
		endif;
        
		$this->db->order_by('training_date','DESC');	
		$Q=$this->db->get($this->training.' t',1);
		//echo $this->db->last_query();
		return $Q->row_array();
		
	}
    
    function getAllRecords($cat='')
	{
		$sql = "SELECT * 
                FROM ".$this->training;
		if($cat)
			$sql .= " WHERE training_category=".$cat;
        $Q=$this->db->query($sql);        
		return $Q->result_array();
	}
	
	function getAllFileRecords($id='')
	{
		$sql = "SELECT * 
                FROM ".$this->files;
		$sql.= " WHERE file_training = ".$id;		
        $Q=$this->db->query($sql);        
		return $Q->result_array();
	}
    
    function getDropdownCategories(){
		$data     = array();
		$query    = $this->db->get($this->categories);
		if ($query->num_rows() > 0):
           foreach ($query->result_array() as $row):
             $data[$row['category_id']] = $row['category_name'];
           endforeach;  
		endif;		
		return $data;
	}
    
    function saveTraining(){
		$value =  array( 'training_title'  	=>  $this->input->post('title'),
						 'training_date'  	=>  $this->input->post('year').'-'.$this->input->post('month').'-'.$this->input->post('day'),
						 'training_category'=>	$this->input->post('category'),
						 'training_content' =>	$this->input->post('content'),
						 'EmployeeID'	    =>	$this->session->userdata('user_login'));
		$this->db->insert($this->training,$value);				 
	}
	
	function saveFile($file=''){
		$value =  array( 'file_title'  	=>  $this->input->post('title'),
						 'file_training'=>  $this->input->post('id'),
						 'file_name'	=>	$file,
						 'file_date'  	=>  date('Y-m-d H:i:s'),
						 'file_type'    =>	'pdf',
						 'file_content' =>	$this->input->post('content'),
						 'EmployeeID'	=>	$this->session->userdata('user_login'));
		$this->db->insert($this->files,$value);				 
	}
    
    function updateTraining(){
		$value =  array( 'training_title'  	=>  $this->input->post('title'),
						 'training_date'  	=>  $this->input->post('year').'-'.$this->input->post('month').'-'.$this->input->post('day'),
						 'training_category'=>	$this->input->post('category'),
						 'training_content' =>	$this->input->post('content'));			 
		$this->db->where('training_id',$this->input->post('id'));				 
		$this->db->update($this->training,$value);				 
	}
	
	function updateMaterial(){
		$query = "UPDATE training SET training_files=training_files + 1 WHERE training_id=".$this->input->post('id');
		$this->db->query($query);
	}
    
    function removeTraining($id){
		$this->db->where('training_id',TRIM($id));				 
		$this->db->delete($this->training);				 
	}
	
	function removeFile($id){
		$this->db->where('file_id',TRIM($id));				 
		$this->db->delete($this->files);				 
	}
	
	function saveCategory(){
		$value =  array( 'category_name'  	=>  $this->input->post('name'));
		$this->db->insert($this->categories,$value);				 
	}
	
	function removeCategory($id){
		$this->db->where('category_id',TRIM($id));				 
		$this->db->delete($this->categories);				 
	}
    
}    